<template>
<view class="content">
	<view :style='{"width":"100%","padding":"0","position":"relative","background":"#f8f8f8","height":"100%"}'>
		<form :style='{"width":"100%","padding":"0 0 48rpx","background":"none","display":"block","height":"auto"}' class="app-update-pv">
			<view :style='{"padding":"12rpx 40rpx","margin":"0 0 8rpx 0","borderColor":"#ccc","alignItems":"center","borderRadius":"0","borderWidth":"0 0 0px 0","background":"#fff","display":"flex","width":"100%","borderStyle":"solid","height":"auto"}' class="">
				<view :style='{"padding":"0 20rpx 0 0","whiteSpace":"nowrap","color":"#333","textAlign":"right","width":"auto","lineHeight":"80rpx","fontSize":"28rpx","fontWeight":"600"}' class="title">酒店名称</view>
				<input :style='{"border":"0","padding":"0px 24rpx","margin":"0px","color":"rgb(0, 0, 0)","borderRadius":"8rpx","flex":"1","background":"rgba(255, 255, 255, 0)","fontSize":"28rpx","height":"80rpx"}' :disabled="ro.hotelname" v-model="ruleForm.hotelname" placeholder="酒店名称"  type="text"></input>
			</view>
			<view :style='{"padding":"12rpx 40rpx","margin":"0 0 8rpx 0","borderColor":"#ccc","alignItems":"center","borderRadius":"0","borderWidth":"0 0 0px 0","background":"#fff","display":"flex","width":"100%","borderStyle":"solid","height":"auto"}' class=" select">
				<view :style='{"padding":"0 20rpx 0 0","whiteSpace":"nowrap","color":"#333","textAlign":"right","width":"auto","lineHeight":"80rpx","fontSize":"28rpx","fontWeight":"600"}' class="title">酒店分类</view>
				<picker :disabled="ro.hotelclassification" :style='{"width":"100%","flex":"1","height":"auto"}' @change="hotelclassificationChange" :value="hotelclassificationIndex" :range="hotelclassificationOptions">
					<view :style='{"width":"100%","lineHeight":"80rpx","fontSize":"28rpx","color":"#23baf7"}' class="uni-input">{{ruleForm.hotelclassification?ruleForm.hotelclassification:"请选择酒店分类"}}</view>
				</picker>
			</view>
			<view :style='{"padding":"12rpx 40rpx","margin":"0 0 8rpx 0","borderColor":"#ccc","alignItems":"center","borderRadius":"0","borderWidth":"0 0 0px 0","background":"#fff","display":"flex","width":"100%","borderStyle":"solid","height":"auto"}' class="" @tap="hotelimageTap">
				<view :style='{"padding":"0 20rpx 0 0","whiteSpace":"nowrap","color":"#333","textAlign":"right","width":"auto","lineHeight":"80rpx","fontSize":"28rpx","fontWeight":"600"}' class="title">酒店图片</view>
				<image :style='{"width":"80rpx","borderRadius":"100%","objectFit":"cover","display":"block","height":"80rpx"}' class="avator" v-if="ruleForm.hotelimage" :src="baseUrl+ruleForm.hotelimage.split(',')[0]" mode="aspectFill"></image>
				<image :style='{"width":"80rpx","borderRadius":"100%","objectFit":"cover","display":"block","height":"80rpx"}' class="avator" v-else src="../../static/gen/upload.png" mode="aspectFill"></image>
			</view>
			<view :style='{"padding":"12rpx 40rpx","margin":"0 0 8rpx 0","borderColor":"#ccc","alignItems":"center","borderRadius":"0","borderWidth":"0 0 0px 0","background":"#fff","display":"flex","width":"100%","borderStyle":"solid","height":"auto"}' class="">
				<view :style='{"padding":"0 20rpx 0 0","whiteSpace":"nowrap","color":"#333","textAlign":"right","width":"auto","lineHeight":"80rpx","fontSize":"28rpx","fontWeight":"600"}' class="title">酒店地址</view>
				<input :style='{"border":"0","padding":"0px 24rpx","margin":"0px","color":"rgb(0, 0, 0)","borderRadius":"8rpx","flex":"1","background":"rgba(255, 255, 255, 0)","fontSize":"28rpx","height":"80rpx"}' :disabled="ro.hoteladdress" v-model="ruleForm.hoteladdress" placeholder="酒店地址"  type="text"></input>
			</view>
			<view :style='{"padding":"12rpx 40rpx","margin":"0 0 8rpx 0","borderColor":"#ccc","alignItems":"center","borderRadius":"0","borderWidth":"0 0 0px 0","background":"#fff","display":"flex","width":"100%","borderStyle":"solid","height":"auto"}' class="">
				<view :style='{"padding":"0 20rpx 0 0","whiteSpace":"nowrap","color":"#333","textAlign":"right","width":"auto","lineHeight":"80rpx","fontSize":"28rpx","fontWeight":"600"}' class="title">酒店电话</view>
				<input :style='{"border":"0","padding":"0px 24rpx","margin":"0px","color":"rgb(0, 0, 0)","borderRadius":"8rpx","flex":"1","background":"rgba(255, 255, 255, 0)","fontSize":"28rpx","height":"80rpx"}' :disabled="ro.hotelphone" v-model="ruleForm.hotelphone" placeholder="酒店电话"  type="text"></input>
			</view>
			<view :style='{"padding":"12rpx 40rpx","margin":"0 0 8rpx 0","borderColor":"#ccc","alignItems":"center","borderRadius":"0","borderWidth":"0 0 0px 0","background":"#fff","display":"flex","width":"100%","borderStyle":"solid","height":"auto"}' class=" select">
				<view :style='{"padding":"0 20rpx 0 0","whiteSpace":"nowrap","color":"#333","textAlign":"right","width":"auto","lineHeight":"80rpx","fontSize":"28rpx","fontWeight":"600"}' class="title">房间类型</view>
				<picker :disabled="ro.roomtype" :style='{"width":"100%","flex":"1","height":"auto"}' @change="roomtypeChange" :value="roomtypeIndex" :range="roomtypeOptions">
					<view :style='{"width":"100%","lineHeight":"80rpx","fontSize":"28rpx","color":"#23baf7"}' class="uni-input">{{ruleForm.roomtype?ruleForm.roomtype:"请选择房间类型"}}</view>
				</picker>
			</view>
			<view :style='{"padding":"12rpx 40rpx","margin":"0 0 8rpx 0","borderColor":"#ccc","alignItems":"center","borderRadius":"0","borderWidth":"0 0 0px 0","background":"#fff","display":"flex","width":"100%","borderStyle":"solid","height":"auto"}' class="">
				<view :style='{"padding":"0 20rpx 0 0","whiteSpace":"nowrap","color":"#333","textAlign":"right","width":"auto","lineHeight":"80rpx","fontSize":"28rpx","fontWeight":"600"}' class="title">房间价格</view>
				<input :style='{"border":"0","padding":"0px 24rpx","margin":"0px","color":"rgb(0, 0, 0)","borderRadius":"8rpx","flex":"1","background":"rgba(255, 255, 255, 0)","fontSize":"28rpx","height":"80rpx"}' :disabled="ro.roomprice" v-model.number="ruleForm.roomprice" placeholder="房间价格" type="digit"></input>
			</view>
			<view :style='{"padding":"12rpx 40rpx","margin":"0 0 8rpx 0","borderColor":"#ccc","alignItems":"center","borderRadius":"0","borderWidth":"0 0 0px 0","background":"#fff","display":"flex","width":"100%","borderStyle":"solid","height":"auto"}' class=" select">
				<view :style='{"padding":"0 20rpx 0 0","whiteSpace":"nowrap","color":"#333","textAlign":"right","width":"auto","lineHeight":"80rpx","fontSize":"28rpx","fontWeight":"600"}' class="title">房间状态</view>
				<picker :disabled="ro.roomstatus" :style='{"width":"100%","flex":"1","height":"auto"}' @change="roomstatusChange" :value="roomstatusIndex" :range="roomstatusOptions">
					<view :style='{"width":"100%","lineHeight":"80rpx","fontSize":"28rpx","color":"#23baf7"}' class="uni-input">{{ruleForm.roomstatus?ruleForm.roomstatus:"请选择房间状态"}}</view>
				</picker>
			</view>
             
			<view :style='{"padding":"10rpx 20rpx","margin":"0","borderColor":"#ccc","borderRadius":"0","borderWidth":"0 0 0px 0","background":"#fff","width":"100%","borderStyle":"solid","height":"auto"}' class="">
				<view :style='{"width":"100%","lineHeight":"80rpx","fontSize":"28rpx","color":"#333","fontWeight":"600"}' class="title">酒店介绍</view>
				<xia-editor ref="hotelintroduction" :style='{"minHeight":"300rpx","border":"0px solid #efefef","width":"100%","height":"auto"}' v-model="ruleForm.hotelintroduction" placeholder="酒店介绍" @editorChange="hotelintroductionChange"></xia-editor>
			</view>
			
			<view :style='{"width":"100%","margin":"40rpx 0","justifyContent":"center","display":"flex","height":"auto"}' class="btn" >
				<button :style='{"border":"0","padding":"0","margin":"0 40rpx 0 0","color":"rgb(255, 255, 255)","borderRadius":"8rpx","background":"#23baf7","width":"250rpx","lineHeight":"80rpx","fontSize":"32rpx","height":"80rpx"}' @tap="onSubmitTap" class="bg-red">提交</button>
			</view>
		</form>

		<w-picker  mode="dateTime" step="1" :current="false" :hasSecond="false" @confirm="clicktimeConfirm" ref="clicktime" themeColor="#333333"></w-picker>
	</view>
</view>
</template>

<script>
	import wPicker from "@/components/w-picker/w-picker.vue";
	import xiaEditor from '@/components/xia-editor/xia-editor';
	import multipleSelect from "@/components/momo-multipleSelect/momo-multipleSelect";
	export default {
		data() {
			return {
				cross:'',
				ruleForm: {
				hotelname: '',
				hotelclassification: '',
				hotelimage: '',
				hoteladdress: '',
				hotelphone: '',
				roomtype: '',
				roomprice: '',
					roomstatus: '未预约' ,
				hotelintroduction: '',
				discussnum: '',
				storeupnum: '',
				},
				hotelclassificationOptions: [],
				hotelclassificationIndex: 0,
				roomtypeOptions: [],
				roomtypeIndex: 0,
				roomstatusOptions: [],
				roomstatusIndex: 0,
				// 登录用户信息
				user: {},
				ro:{
				   hotelname : false,
				   hotelclassification : false,
				   hotelimage : false,
				   hoteladdress : false,
				   hotelphone : false,
				   roomtype : false,
				   roomprice : false,
				   roomstatus : false,
				   hotelintroduction : false,
				   clicktime : false,
				   clicknum : false,
				   discussnum : false,
				   storeupnum : false,
				},
				virtualPay: false,
			}
		},
		components: {
			wPicker,
			xiaEditor,
			multipleSelect,
		},
		computed: {
			baseUrl() {
				return this.$base.url;
			},



		},
		async onLoad(options) {
			if(options.virtualPay){
				this.virtualPay = true
			}
			let table = uni.getStorageSync("nowTable");
			// 获取用户信息
			let res = await this.$api.session(table);
			this.user = res.data;
			
			// ss读取

			this.ro.roomstatus = true;

			// 下拉框
			res = await this.$api.option(`hotelclassification`,`hotelclassification`,{});
			this.hotelclassificationOptions = res.data;
			this.hotelclassificationOptions.unshift("请选择酒店分类");
			// 自定义下拉框值
			this.roomtypeOptions = "标准房,双人房,大床房".split(',')
			// 自定义下拉框值
			this.roomstatusOptions = "已预约,未预约".split(',')

			// 如果有登录，获取登录后保存的userid
			this.ruleForm.userid = uni.getStorageSync("appUserid")
			if (options.refid) {
				// 如果上一级页面传递了refid，获取改refid数据信息
				this.ruleForm.refid = Number(options.refid);
				this.ruleForm.nickname = uni.getStorageSync("nickname");
			}
			// 如果是更新操作
			if (options.id) {
				this.ruleForm.id = options.id;
				// 获取信息
				res = await this.$api.info(`scenicspothotels`, this.ruleForm.id);
				this.ruleForm = res.data;
			}
			// 跨表
			this.cross = options.cross;
			if(options.cross){
				var obj = uni.getStorageSync('crossObj');
				for (var o in obj){
					if(o=='hotelname'){
						this.ruleForm.hotelname = obj[o];
						this.ro.hotelname = true;
						continue;
					}
					if(o=='hotelclassification'){
						this.ruleForm.hotelclassification = obj[o];
						this.ro.hotelclassification = true;
						continue;
					}
					if(o=='hotelimage'){
						this.ruleForm.hotelimage = obj[o].split(",")[0];
						this.ro.hotelimage = true;
						continue;
					}
					if(o=='hoteladdress'){
						this.ruleForm.hoteladdress = obj[o];
						this.ro.hoteladdress = true;
						continue;
					}
					if(o=='hotelphone'){
						this.ruleForm.hotelphone = obj[o];
						this.ro.hotelphone = true;
						continue;
					}
					if(o=='roomtype'){
						this.ruleForm.roomtype = obj[o];
						this.ro.roomtype = true;
						continue;
					}
					if(o=='roomprice'){
						this.ruleForm.roomprice = obj[o];
						this.ro.roomprice = true;
						continue;
					}
					if(o=='roomstatus'){
						this.ruleForm.roomstatus = obj[o];
						this.ro.roomstatus = true;
						continue;
					}
					if(o=='hotelintroduction'){
						this.ruleForm.hotelintroduction = obj[o];
						this.ro.hotelintroduction = true;
						continue;
					}
					if(o=='clicktime'){
						this.ruleForm.clicktime = obj[o];
						this.ro.clicktime = true;
						continue;
					}
					if(o=='clicknum'){
						this.ruleForm.clicknum = obj[o];
						this.ro.clicknum = true;
						continue;
					}
					if(o=='discussnum'){
						this.ruleForm.discussnum = obj[o];
						this.ro.discussnum = true;
						continue;
					}
					if(o=='storeupnum'){
						this.ruleForm.storeupnum = obj[o];
						this.ro.storeupnum = true;
						continue;
					}
				}
			}
			this.styleChange()
			this.$forceUpdate()
			if (uni.getStorageSync('raffleType') && uni.getStorageSync('raffleType') != null) {
				uni.removeStorageSync('raffleType')
				setTimeout(() => {
					this.onSubmitTap()
				}, 300)
			}
		},
		methods: {
			hotelintroductionChange(e) {
				this.ruleForm.hotelintroduction = e
			},
			styleChange() {
				this.$nextTick(()=>{
					// document.querySelectorAll('.app-update-pv . .uni-input-input').forEach(el=>{
					//   el.style.backgroundColor = this.addUpdateForm.input.content.backgroundColor
					// })
				})
			},

			// 多级联动参数


			// 日长控件选择日期时间
			clicktimeConfirm(val) {
				console.log(val)
				this.ruleForm.clicktime = val.result;
				this.$forceUpdate();
			},

			// 下拉变化
			hotelclassificationChange(e) {
				this.hotelclassificationIndex = e.target.value
				this.ruleForm.hotelclassification = this.hotelclassificationOptions[this.hotelclassificationIndex]
			},
			// 下拉变化
			roomtypeChange(e) {
				this.roomtypeIndex = e.target.value
				this.ruleForm.roomtype = this.roomtypeOptions[this.roomtypeIndex]
			},
			// 下拉变化
			roomstatusChange(e) {
				this.roomstatusIndex = e.target.value
				this.ruleForm.roomstatus = this.roomstatusOptions[this.roomstatusIndex]
			},

			hotelimageTap() {
				let _this = this;
				this.$api.upload(function(res) {
					_this.ruleForm.hotelimage = 'upload/' + res.file;
					_this.$forceUpdate();
					_this.$nextTick(()=>{
						_this.styleChange()
					})
				});
			},

			getUUID () {
				return new Date().getTime();
			},
			async onSubmitTap() {
				let that = this
				//跨表计算判断
				var obj;
				if((!this.ruleForm.hotelname)){
					this.$utils.msg(`酒店名称不能为空`);
					return
				}
				if(this.ruleForm.hotelphone&&(!this.$validate.isMobile(this.ruleForm.hotelphone))){
					this.$utils.msg(`酒店电话应输入手机格式`);
					return
				}
				if(this.ruleForm.roomprice&&(!this.$validate.isNumber(this.ruleForm.roomprice))){
					this.$utils.msg(`房间价格应输入数字`);
					return
				}
				if(this.ruleForm.clicknum&&(!this.$validate.isIntNumer(this.ruleForm.clicknum))){
					this.$utils.msg(`点击次数应输入整数`);
					return
				}
				if(this.ruleForm.discussnum&&(!this.$validate.isIntNumer(this.ruleForm.discussnum))){
					this.$utils.msg(`评论数应输入整数`);
					return
				}
				if(this.ruleForm.storeupnum&&(!this.$validate.isIntNumer(this.ruleForm.storeupnum))){
					this.$utils.msg(`收藏数应输入整数`);
					return
				}
				//更新跨表属性
				var crossuserid;
				var crossrefid;
				var crossoptnum;
				if(this.cross){
					var statusColumnName = uni.getStorageSync('statusColumnName');
					var statusColumnValue = uni.getStorageSync('statusColumnValue');
					if(statusColumnName!='') {
						if(!obj) {
							obj = uni.getStorageSync('crossObj');
						}
						if(!statusColumnName.startsWith("[")) {
							for (var o in obj){
								if(o==statusColumnName){
									obj[o] = statusColumnValue;
								}

							}
							var table = uni.getStorageSync('crossTable');
							await this.$api.update(`${table}`, obj);
						} else {
							   crossuserid=Number(uni.getStorageSync('appUserid'));
							   crossrefid=obj['id'];
							   crossoptnum=uni.getStorageSync('statusColumnName');
							   crossoptnum=crossoptnum.replace(/\[/,"").replace(/\]/,"");
						}
					}
				}
				if(crossrefid && crossuserid) {
					this.ruleForm.crossuserid=crossuserid;
					this.ruleForm.crossrefid=crossrefid;
					let params = {
						page: 1,
						limit:10,
						crossuserid:crossuserid,
						crossrefid:crossrefid,
					}
					let res = await this.$api.list(`scenicspothotels`, params);
					if (res.data.total >= crossoptnum) {
						this.$utils.msg(uni.getStorageSync('tips'));
						uni.removeStorageSync('crossCleanType');
						return false;
					} else {
				//跨表计算
						let oet = {}
						if(this.ruleForm.id){
							await this.$api.update(`scenicspothotels`, this.ruleForm);
						}else{
							oet = await this.$api.add(`scenicspothotels`, this.ruleForm);
						}
						if(this.cross){
							uni.setStorageSync('crossCleanType',true);
						}
						this.$utils.msgBack('提交成功');
					}
				} else {
				//跨表计算
					let oet = {}
					if(this.ruleForm.id){
						await this.$api.update(`scenicspothotels`, this.ruleForm);
					}else{
						oet = await this.$api.add(`scenicspothotels`, this.ruleForm);
					}
					if(this.cross){
						uni.setStorageSync('crossCleanType',true);
					}
					this.$utils.msgBack('提交成功');
				}
			},
			optionsChange(e) {
				this.index = e.target.value
			},
			bindDateChange(e) {
				this.date = e.target.value
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			toggleTab(str) {
				if(this.ro[str]){
					return false
				}
				this.$refs[str].show();
			},
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		min-height: calc(100vh - 44px);
		box-sizing: border-box;
	}
</style>
